package com.alcj.service.impl;

import com.alcj.common.constant.SysConstant;
import com.alcj.common.utils.CmnUtil;
import com.alcj.mapper.DetectReportDetailMapper;
import com.alcj.mapper.DetectReportMapper;
import com.alcj.mapper.NewsMapper;
import com.alcj.model.DetectReport;
import com.alcj.model.DetectReportDetail;
import com.alcj.model.News;
import com.alcj.service.DetectReportService;
import com.alcj.service.NewsService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :chenxh
 * @version :v1.0
 * @Package :com.alcj.service.impl
 * @Description :${TODO}(新闻资讯数据处理)
 * @date :2017/9/5 11:23
 */
@Service
public class DetectReportServiceImpl implements DetectReportService {

    @Autowired
    DetectReportMapper detectReportMapper;
    @Autowired
    DetectReportDetailMapper detectReportDetailMapper;
    /**
     * @Author: chenxh
     * @Description:查询获取检测报告
     * @Date: 2017/9/7 16:29
     * @param: record
     */
    @Override
    public int getReprotCount(DetectReport recode) throws Exception{

        // 返回结果
        return this.detectReportMapper.selectReprotCount(recode);
    }

    /**
     * @Author: chenxh
     * @Description:查询获取检测报告
     * @Date: 2017/9/7 16:29
     * @param: record
     */
    @Override
    public List<DetectReport> getReprotList(DetectReport recode) throws Exception{

        // 返回结果
        return this.detectReportMapper.selectReprotList(recode);
    }

    /**
     * @Author: chenxh
     * @Description:查询获取检测报告详细数据
     * @Date: 2017/9/7 16:29
     * @param: record
     */
    @Override
    public JSONObject getReprotInfo(DetectReport recode) throws Exception{
        // 结果集
        JSONObject resultJson = new JSONObject();
        // 详细项目集合
        Map resultMap= new HashMap();
        // 获取基本项目信息
        DetectReport report = this.detectReportMapper.selectByPrimaryKey(recode.getReportid());
        // 获取详细项目集合
        List<DetectReportDetail> reportDetail = this.detectReportDetailMapper.selectDetailType(recode.getReportid());
        // 最后一个仪器项目组
        List<DetectReportDetail> lastGroup = new ArrayList<DetectReportDetail>();
        // 最后一个仪器项目集合
        List<List<DetectReportDetail>> lastList = new ArrayList<List<DetectReportDetail>>();
        for(DetectReportDetail reportItem : reportDetail){
            // 获取大分类项目集合
            List<DetectReportDetail> reportDetailItem = this.detectReportDetailMapper.selectDetailByType(reportItem);
            List<DetectReportDetail> tempGroup = new ArrayList<DetectReportDetail>();
            List<List<DetectReportDetail>> reportTypeList = new ArrayList<List<DetectReportDetail>>();
            if(!CmnUtil.isEmpty(reportDetailItem)){
                // 判断是否为仪器检测项目
                if(CmnUtil.isEmpty(reportDetailItem.get(0).getItemParam())){
                    // 普通检测项目
                    for (int index = 0 ; index <= reportDetailItem.size()-1; index++){
                        // 一行显示两个，第一个时
                        if (index % 2 == 0){
                            // 创建新的检测项目分组
                            tempGroup = new ArrayList<DetectReportDetail>();
                            // 将检测项目放到检测项目分组中
                            tempGroup.add(reportDetailItem.get(index));
                            // 最后一行时
                            if (index + 1 > reportDetailItem.size() -1 ){
                                // 将检测项目放到检测项目分组列表中
                                reportTypeList.add(tempGroup);
                            }
                        }else{
                            // 将检测项目放到检测项目分组中
                            tempGroup.add(reportDetailItem.get(index));
                            // 将检测项目放到检测项目分组列表中
                            reportTypeList.add(tempGroup);
                        }
                    }
                    // 将一类检测项目放到检测报告详细列表中
                    resultMap.put(reportItem.getCategory(),reportTypeList);
                }else{
                    // 仪器检测项目
                    lastGroup = reportDetailItem;
                    for (int index = 0 ; index <= reportDetailItem.size()-1; index++){
                        // 一行显示两个，第一个时
                        if (index % 2 == 0){
                            // 创建新的检测项目分组
                            lastGroup = new ArrayList<DetectReportDetail>();
                            // 将检测项目放到检测项目分组中
                            lastGroup.add(reportDetailItem.get(index));
                            // 最后一行时
                            if (index + 1 > reportDetailItem.size() -1 ){
                                // 将检测项目放到检测项目分组列表中
                                lastList.add(lastGroup);
                            }
                        }else{
                            // 将检测项目放到检测项目分组中
                            lastGroup.add(reportDetailItem.get(index));
                            // 将检测项目放到检测项目分组列表中
                            lastList.add(lastGroup);
                        }
                    }
                }

            }

        }
        // 检测报告基础数据
        resultJson.put("report",report);
        // 检测报告详细列表
        resultJson.put("reportList",reportDetail);
        // 检测报告详细列表
        resultJson.put("reportDetail",resultMap);
        // 检测报告仪器设备详细列表
        resultJson.put("lastList",lastList);
        // 返回结果
        return resultJson;
    }

    /**
     * @Author: chenxh
     * @Description:查询获取车况分析详细数据
     * @Date: 2017/9/7 16:29
     * @param: record
     */
    @Override
    public JSONObject getVehicleInfo(DetectReport recode) throws Exception{
        // 结果集
        JSONObject resultJson = new JSONObject();
        // 详细项目集合
        Map resultMap= new HashMap();
        // 获取最近的一次车辆检测报告
        DetectReport nowReport = this.detectReportMapper.selectNowReprot(recode);
        if(CmnUtil.isEmpty(nowReport)){
            return null;
        }
        // 设定检测分析报告id
        recode.setReportid(nowReport.getReportid());
        // 获取基本项目信息
        DetectReport report = this.detectReportMapper.selectByPrimaryKey(recode.getReportid());
        // 获取详细项目集合
        List<DetectReportDetail> reportDetail = this.detectReportDetailMapper.selectDetailType(recode.getReportid());
        // 设备仪器检测项目集合
        Map lastMap= new HashMap();
        // 仪器检测项目
        List<DetectReportDetail> suggestList  = new ArrayList<DetectReportDetail>();
        for(DetectReportDetail reportItem : reportDetail){
            // 获取大分类项目集合
            List<DetectReportDetail> reportDetailItem = this.detectReportDetailMapper.selectDetailByType(reportItem);
            if(!CmnUtil.isEmpty(reportDetailItem)){
                // 判断是否为仪器检测项目
                if(CmnUtil.isEmpty(reportDetailItem.get(0).getItemParam())){
                    // 普通检测项目
                    // 将一类检测项目放到检测报告详细列表中
                    resultMap.put(reportItem.getCategory(),reportDetailItem);
                }else{
                    // 仪器检测项目
                    List<DetectReportDetail> lastDetailItem = this.detectReportDetailMapper.selectDetailItem(reportItem);
                    suggestList = lastDetailItem;
                    for (DetectReportDetail lastItem:lastDetailItem){
                        List<DetectReportDetail> lastGroupItem = this.detectReportDetailMapper.selectDetailByItem(lastItem);
                        // 将检测项目放到检测项目分组列表中
                        lastMap.put(lastItem.getItemName(),lastGroupItem);
                    }
                }

            }

        }
        // 检测报告基础数据
        resultJson.put("report",report);
        // 检测报告详细列表
        resultJson.put("reportList",reportDetail);
        // 检测报告详细列表
        resultJson.put("reportDetail",resultMap);
        // 检测报告仪器设备详细列表
        resultJson.put("lastList",lastMap);
        // 检测报告仪器设备建议详细列表
        resultJson.put("suggestList",suggestList);
        // 返回结果
        return resultJson;
    }
}
